package com.jiuwu.note;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jiuwu.note.po.HotGoodsPO;
import com.jiuwu.note.service.GoodsService;

@RequestMapping("/goods")
@Controller
public class GoodsController {
	Logger logger = Logger.getLogger(GoodsController.class);
	
	@Autowired
	GoodsService goodsService;
	
	@RequestMapping("/add")
	public ModelAndView addGoods(@RequestParam("goodsname") String goodsName,
			@RequestParam(value="goodsnameen",required=false) String goodsNameEnglish,
			@RequestParam(value="maningprice",required=false) Float maningPrice) {
		
		//ModelAndView modelAndView = new ModelAndView();
		HotGoodsPO goods = new HotGoodsPO();
		goods.setGoodName(goodsName);
		goods.setGoodNameEn(goodsNameEnglish);
		goods.setManingPrice(maningPrice);
		
		goods.setAddTime(new Date());
		
		int rows = goodsService.addGoods(goods);
		if(rows <= 0) {
			logger.error("添加失败");
		}else {
			logger.debug("商品添加成功："+goods.toString());
		}
		return new ModelAndView("redirect:/goods/show");
		//modelAndView.setViewName("showAllGoods");
		//return showAllGoods();
	}
	
	
	@RequestMapping("/show")
	public ModelAndView showAllGoods() {
		ModelAndView modelAndView = new ModelAndView();
		List<HotGoodsPO> list = goodsService.getAllGoods();
		modelAndView.addObject("goodsList", list);
		modelAndView.setViewName("showAllGoods");
		return modelAndView;
	}
	
	

}
